package com.tr.syscms.controller;


import com.tr.common.util.Result;
import com.tr.common.util.ResultStatus;
import com.tr.syscms.entity.Role;
import com.tr.syscms.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author xiijava
 * @since 2022-06-07
 */
//@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;


    /**
     * 根据用户ID获取角色ID集合
     * @param userId
     * @return
     */
    @GetMapping("queryRoleIdsByUserId")
    public Result queryRoleIdsByUserId(Integer userId) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.roleService.queryRoleIdsByUserId(userId));
    }
    /**
     * 添加用户角色关联方法
     * @param userId
     * @param roleIds
     * @return
     */
    @PutMapping("addUserAndRole")
    public Result addUserAndRole(Integer userId,String roleIds) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.roleService.addUserAndRole(userId,roleIds));
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.roleService.queryById(id));
    }

    /**
     * 分页带参数查询
     *
     * @param map 参数
     * @return
     */
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody Map map) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.roleService.queryAll(map));
    }

    /**
     * 添加
     *
     * @param role
     * @return
     */
    @PutMapping("add")
    public Result add(@RequestBody Role role) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.roleService.insert(role));
    }

    /**
     * 更新
     *
     * @param role
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody Role role) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.roleService.update(role));
    }

    /**
     * 根据编号删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.roleService.deleteById(id));
    }
}

